package com.share.admin.web;

import com.share.admin.config.security.SecurityContext;
import com.share.admin.model.db.Menu;
import com.share.admin.model.vo.MenuAddOrUpdateReq;
import com.share.admin.model.vo.MenuPageReq;
import com.share.admin.model.vo.MenuVo;
import com.share.admin.model.vo.RolePageReq;
import com.share.admin.service.IMenuService;
import com.share.common.common.Page;
import com.share.common.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * <p>
 * 角色菜单关系表  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-09-22
 */
@Slf4j
@Api("菜单模块")
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private IMenuService iMenuService;


    @ApiOperation("查询某个菜单下权限")
    @RequestMapping("/listPermissionByMenuId")
    public Result listPermissionByMenuId(Long menuId) {
        Set<String> set = null;
        if (SecurityContext.isAdmin()) {
            set = iMenuService.permissionByMenuId(menuId);
        } else {
            Long userId = SecurityContext.getUserId();
            set = iMenuService.permissionByMenuIdAndUserId(menuId, userId);
        }
        return Result.success(set);
    }


    @ApiOperation("查询用户的菜单tree")
    @RequestMapping("/treeMenu")
    public Result treeMenu() {
        Set<MenuVo> set = null;
        if (SecurityContext.isAdmin()) {
            set = iMenuService.treeAll();
        } else {
            Long userId = SecurityContext.getUserId();
            set = iMenuService.treeByUserId(userId);
        }
        return Result.success(set);
    }

    @ApiOperation("查询菜单")
    @RequestMapping("/list")
    public Result list() {
        Set<Menu> set = null;
        if (SecurityContext.isAdmin()) {
            set = iMenuService.listAll();
        } else {
            Long userId = SecurityContext.getUserId();
            set = iMenuService.listByUserId(userId);
        }
        return Result.success(set);
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageList")
    public Result resultPageList(MenuPageReq req) {
        Page page = iMenuService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(MenuAddOrUpdateReq req) {
        Menu record = new Menu();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iMenuService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(MenuAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        Menu record = new Menu();
        BeanUtils.copyProperties(req, record);
        return Result.bool(iMenuService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(iMenuService.removeById(id));
    }
}
